import request from "@/utils/request";
import axios from "axios";
import {ElMessage} from "element-plus";

/**
 * 获取头像预签名上传的URL
 * @param fileName
 */
export function getHeadUploadPresigned(fileName) {
    return request.get(
        `/file/head/token?fileName=${fileName}`
    );
}
/**
 * 获取公告预签名上传的URL
 * @param fileName
 */
export function getInformUploadPresigned(fileName) {
    return request.get(
        `/file/inform/token?fileName=${fileName}`
    );
}
/**
 * 获取Logo预签名上传的URL
 * @param fileName
 */
export function getLogoUploadPresigned(fileName) {
    return request.get(
        `/file/logo/token?fileName=${fileName}`
    );
}
/**
 * 获取Project预签名上传的URL
 * @param fileName
 */
export function getProjectUploadPresigned(fileName) {
    return request.get(
        `/file/project/token?fileName=${fileName}`
    );
}
/**
 * 获取Receipt预签名上传的URL
 * @param fileName
 */
export function getReceiptUploadPresigned(fileName) {
    return request.get(
        `/file/receipt/token?fileName=${fileName}`
    );
}

/**
 * 获取预签名上传的URL
 * @param fileName
 */
export function getUploadPresigned(fileName) {
    return request.get(
        `/file/token?fileName=${fileName}`
    );
}

export function uploadByPresignedUrl(url, file) {
    console.log(url,file)
    return axios.put(url, file,
        {
            headers: {
                "Content-Type": 'multipart/form-data', //向minio传文件,必须设置内容类型,否则minio无法预览
            }
        }).then(data=>{
            let a=data.data
        if(typeof a !== 'undefined' && a != null && a !== '') {
            console.log(data.data);
            ElMessage.error('文件上传错误')
            throw Error('文件上传错误');
        }
    }).catch(e=>{
        ElMessage.error('文件上传错误')
        throw Error(e);
    })
}
